package com.microsoft.mcp.sample.server.service;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;

/**
 * 计算服务
 * 通过MCP 提供基础的计算功能
 */
@Service
public class CalculatorService {

  @Tool(description = "Add two numbers together")
  public String add(double a, double b) {
    double result = a + b;
    return formatResult(a, "+", b , result);
  }

  @Tool(description = "Subtract the second number from the first number")
  public String subtract(double a, double b) {
    double result = a - b;
    return formatResult(a, "-", b , result);
  }

  @Tool(description = "Multiply tow numbers together")
  public String multiply(double a, double b) {
    double result = a * b;
    return formatResult(a, "*", b , result);
  }

  @Tool(description = "Divide the first number by the second number")
  public String divide(double a, double b) {
    if (b == 0) {
      return "Error: Cannot divided by zero";
    }
    double result = a / b;
    return formatResult(a, "/", b , result);
  }

  @Tool(description = "Calculate the power of a number (base raised to an exponent)")
  public String power(double base, double exponent) {
    double result = Math.pow(base, exponent);
    return formatResult(base, "^", exponent , result);
  }

  @Tool(description = "Calculate the square root of a number")
  public String squareRoot(double number) {
    if (number < 0) {
      return "Error: Cannot calculate square root of a negative number";
    }
    double result = Math.sqrt(number);
    return String.format("√%.2f = %.2f", number, result);
  }

  @Tool(description = "Calculate the remainder when one number is divided by another")
  public String modulus(double a, double b) {
    if (b == 0) {
      return "Error: Cannot divide by zero";
    }
    double result = a % b;
    return formatResult(a, "%", b, result);
  }

  @Tool(description = "Calculate the absolute value of a number")
  public String absolute(double number) {
    double result = Math.abs(number);
    return String.format("|%.2f| = %.2f", number, result);
  }

  /**
   * 获取有关可用计算器操作的帮助
   * @return 返回有关可用操作的信息
   */
  @Tool(description = "Get help about available calculator operations")
  public String help() {
    return "Basic Calculator MCP Service\n\n" +
        "Available operations:\n" +
        "1. add(a, b) - Adds two numbers\n" +
        "2. subtract(a, b) - Subtracts the second number from the first\n" +
        "3. multiply(a, b) - Multiplies two numbers\n" +
        "4. divide(a, b) - Divides the first number by the second\n" +
        "5. power(base, exponent) - Raises a number to a power\n" +
        "6. squareRoot(number) - Calculates the square root\n" +
        "7. modulus(a, b) - Calculates the remainder of division\n" +
        "8. absolute(number) - Calculates the absolute value\n\n" +
        "Example usage: add(5, 3) will return 5 + 3 = 8";
  }

  private String formatResult(double a, String operator, double b, double result) {
    return String.format("%.2f %s %.2f = %.2f", a, operator, b, result);
  }
}
